/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.transfer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.modules.transfer.domain.TransferAftt;
import me.zhengjie.tool.NPETool;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.transfer.service.TransferAfttService;
import me.zhengjie.modules.transfer.domain.dto.TransferAfttQueryCriteria;
import me.zhengjie.modules.transfer.mapper.TransferAfttMapper;
import me.zhengjie.vo.Constant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author lzw
* @date 2025-06-13
**/
@Service
@RequiredArgsConstructor
public class TransferAfttServiceImpl extends ServiceImpl<TransferAfttMapper, TransferAftt> implements TransferAfttService {

    private final TransferAfttMapper transferAfttMapper;

    @Override
    public PageResult<TransferAftt> queryAll(TransferAfttQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(transferAfttMapper.findAll(criteria, page));
    }

    @Override
    public List<TransferAftt> queryAll(TransferAfttQueryCriteria criteria){
        return transferAfttMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(TransferAftt resources) {
        transferAfttMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TransferAftt resources) {
        TransferAftt transferAftt = getById(resources.getAaz198());
        transferAftt.copy(resources);
        transferAfttMapper.updateById(transferAftt);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<String> ids) {
        transferAfttMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<TransferAftt> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TransferAftt transferAftt : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("转出地社保机构名称", transferAftt.getAab300());
            map.put("行政区划代码", transferAftt.getAab301());
            map.put("行政区划版本号", transferAftt.getAae383());
            map.put("地址", transferAftt.getAae006());
            map.put("邮政编码", transferAftt.getAae007());
            map.put("备注", transferAftt.getAae013());
            map.put("节点代码", transferAftt.getAaa226());
            map.put("转入地社保机构银行账号", transferAftt.getAac078());
            map.put("转入地社保机构开户行行号", transferAftt.getAac080());
            map.put("转入地社保机构开户银行名称", transferAftt.getAac155());
            map.put("银行户名", transferAftt.getAae009());
            map.put("开始日期", transferAftt.getAae030());
            map.put("终止日期", transferAftt.getAae031());
            map.put("经办时间", transferAftt.getAae036());
            map.put("险种类型", transferAftt.getAae140());
            map.put("入网状态", transferAftt.getAaf003());
            map.put("跨制度入网状态，0：未入网、1：入网、2：暂停", transferAftt.getAaf141());
            map.put("跨制度入网开通时间", transferAftt.getAaf142());
            map.put("经办机构流水号", transferAftt.getAaz408());
            map.put("法定代表人联系电话", transferAftt.getAae005());
            map.put("联系电子邮箱", transferAftt.getAae159());
            map.put("科室名称", transferAftt.getAae386());
            map.put("科室传真", transferAftt.getAae387());
            map.put("科室类型", transferAftt.getAaf304());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List getaftt(Map map) {


        NPETool.isEmptyThrowException(map.get("aic113"), "转移制度方向");
        String aae140="";

        switch (map.get("aic113").toString()){
            case Constant.AIC113_1_QYZGYLBXZDNZY:
                aae140=Constant.AAE140_110_QYZGJBYLBX;

                break;
            case Constant.AIC113_2_QYZGYLBXZDZJGSYDWYLBXZD:
                aae140=Constant.AAE140_120_JGSYDWGZRYJBYLBX;

                break;
            case Constant.AIC113_3_JGSYDWYLBXZDZQYZGYLBXZD:
                aae140=Constant.AAE140_110_QYZGJBYLBX;

                break;
            case Constant.AIC113_6_JGSYDWYLBXZDNZY:
                aae140=Constant.AAE140_120_JGSYDWGZRYJBYLBX;

                break;
            case Constant.AIC113_4_QYZGYLBXZDZCXJMJBYLZD:
                aae140=Constant.AAE140_170_CXJMJBYLBX;

                break;
            case Constant.AIC113_9_ZYNJZY:
                aae140=Constant.AAE140_180_ZYNJ;
                break;
            case Constant.AIC113_10_ZYNJZY:
                aae140=Constant.AAE140_180_ZYNJ;
                break;
            default:
                throw new IllegalArgumentException("没有获取到对应的转出类型！");
        }
        QueryWrapper<TransferAftt> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aab301",map.get("aab301"));
        queryWrapper.eq("aae140",aae140);

        return transferAfttMapper.selectMaps(queryWrapper);
    }
}
